﻿//-----------------------------------------------------------------------
// <Copyright>
// * Copyright (C) 2022 RuYiAdmin All Rights Reserved
// </Copyright>
//-----------------------------------------------------------------------

using Minio.DataModel;
using Minio.DataModel.Args;
using RuYiAdmin.Net.DataModel.EntityDataModel.Sys;
using RuYiAdmin.Net.DataRepository.BusinessRepository.Interface.Sys;
using RuYiAdmin.Net.SystemService.BaseService.Implementation;
using RuYiAdmin.Net.SystemService.BusinessService.Interface.Sys;
using RuYiAdmin.Net.UniversalFoundation.Configurations;
using RuYiAdmin.Net.UniversalFoundation.Contexts;
using RuYiAdmin.Net.UniversalFoundation.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace RuYiAdmin.Net.SystemService.BusinessService.Implementation.Sys
{
    /// <summary>
    /// 业务附件服务层实现
    /// </summary>
    public class SysAttachmentService : RuYiAdminBaseService<SysAttachment>, ISysAttachmentService
    {
        #region 属性及构造函数

        /// <summary>
        /// 业务附件仓储实例
        /// </summary>
        private readonly ISysAttachmentRepository AttachmentRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="AttachmentRepository"></param>
        public SysAttachmentService(ISysAttachmentRepository AttachmentRepository) : base(AttachmentRepository)
        {
            this.AttachmentRepository = AttachmentRepository;
        }

        #endregion

        #region 系统文件统计

        /// <summary>
        /// 系统文件统计（MB）
        /// </summary>
        /// <returns>ActionResponseResult</returns>
        public Task<ActionResponseResult> QuerySysFileStatisticalInfo()
        {
            return Task.Run(async () =>
            {
                //临时文件
                var tempFileLength = RuYiFileContext.GetFileNames(RuYiGlobalConfig.DirectoryConfig.GetTempPath()).Length;
                var tempFileSize = RuYiFileContext.GetDirectoryFileSizeByMB(RuYiGlobalConfig.DirectoryConfig.GetTempPath());

                //审计日志
                int monitoringLogLength = 0;
                double monitoringLogSize = 0;
                if (RuYiGlobalConfig.MinioConfig.IsEnabled)
                {
                    ListObjectsArgs listObjectsArgs = new ListObjectsArgs()
                        .WithBucket(RuYiGlobalConfig.MinioConfig.BucketName)
                        .WithPrefix(RuYiGlobalConfig.DirectoryConfig.GetMinioMonitoringLogsPath())
                        .WithRecursive(true);
                    IAsyncEnumerable<Item> files = RuYiMinioContext.Instance.ListObjectsEnumAsync(listObjectsArgs);
                    await foreach (var file in files)
                    {
                        monitoringLogLength++;
                        monitoringLogSize += file.Size;
                    }
                    monitoringLogSize = monitoringLogSize / (1024.0 * 1024.0); // 转换为MB
                }
                else
                {
                    monitoringLogLength = RuYiFileContext.GetFileNames(RuYiGlobalConfig.DirectoryConfig.GetMonitoringLogsPath()).Length;
                    monitoringLogSize = RuYiFileContext.GetDirectoryFileSizeByMB(RuYiGlobalConfig.DirectoryConfig.GetMonitoringLogsPath());
                }

                //业务附件
                int businessAttachmentLength = 0;
                double businessAttachmentSize = 0;
                if (RuYiGlobalConfig.MinioConfig.IsEnabled)
                {
                    ListObjectsArgs listObjectsArgs = new ListObjectsArgs()
                        .WithBucket(RuYiGlobalConfig.MinioConfig.BucketName)
                        .WithPrefix(RuYiGlobalConfig.DirectoryConfig.GetMinioBusinessAttachmentPath())
                        .WithRecursive(true);
                    IAsyncEnumerable<Item> files = RuYiMinioContext.Instance.ListObjectsEnumAsync(listObjectsArgs);
                    await foreach (var file in files)
                    {
                        businessAttachmentLength++;
                        businessAttachmentSize += file.Size;
                    }
                    businessAttachmentSize = businessAttachmentSize / (1024.0 * 1024.0); // 转换为MB
                }
                else
                {
                    businessAttachmentLength = RuYiFileContext.GetFileNames(RuYiGlobalConfig.DirectoryConfig.GetBusinessAttachmentPath()).Length;
                    businessAttachmentSize = RuYiFileContext.GetDirectoryFileSizeByMB(RuYiGlobalConfig.DirectoryConfig.GetBusinessAttachmentPath());
                }

                var logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RuYiAdminLogs/");

                //Debug文件
                var debugLogLength = RuYiFileContext.GetFileNames(logPath, "Debug*.log", true).Length;
                var debugLogSize = RuYiFileContext.GetDirectoryFileSizeByMB(logPath, "Debug*.log", true);

                //Error文件
                var errorLogLength = RuYiFileContext.GetFileNames(logPath, "Error*.log", true).Length;
                var errorLogSize = RuYiFileContext.GetDirectoryFileSizeByMB(logPath, "Error*.log", true);

                //Info文件
                var infoLogLength = RuYiFileContext.GetFileNames(logPath, "Info*.log", true).Length;
                var infoLogSize = RuYiFileContext.GetDirectoryFileSizeByMB(logPath, "Info*.log", true);

                //Warn文件
                var warnLogLength = RuYiFileContext.GetFileNames(logPath, "Warn*.log", true).Length;
                var warnLogSize = RuYiFileContext.GetDirectoryFileSizeByMB(logPath, "Warn*.log", true);

                var obj = new
                {
                    TempFilesLength = tempFileLength,
                    TempFileSize = tempFileSize,
                    MonitoringLogLength = monitoringLogLength,
                    MonitoringLogSize = monitoringLogSize,
                    BusinessAttachmentLength = businessAttachmentLength,
                    BusinessAttachmentSize = businessAttachmentSize,
                    DebugLogLength = debugLogLength,
                    DebugLogSize = debugLogSize,
                    ErrorLogLength = errorLogLength,
                    ErrorLogSize = errorLogSize,
                    InfoLogLength = infoLogLength,
                    InfoLogSize = infoLogSize,
                    WarnLogLength = warnLogLength,
                    WarnLogSize = warnLogSize
                };

                return ActionResponseResult.Success(obj);
            });
        }

        #endregion
    }
}
